<template>
  <div>

    <div class="detail_title">
      <h4 v-text="pageTitle"></h4>
      <a class="close" @click="close" href="javascript:void(0)">╳</a>
    </div>

    <div class="device_div"></div>

    <userInfo v-bind:info="info" v-bind:insuredArea="insuredArea" v-bind:state="state" v-bind:id="id"></userInfo>

    <div class="device_div"></div>

    <teamAndLevel v-bind:info="info" v-bind:teamInfo="teamInfo" v-bind:levelInfo="levelInfo" v-bind:id="id"></teamAndLevel>

    <div class="device_div"></div>

    <detailInfo v-bind:info="info" v-bind:gender="gender" v-bind:id="id"></detailInfo>

    <div class="device_div"></div>

    <accountInfo v-bind:info="info" v-if="isShowAccount"></accountInfo>

    <div class="bottom_btn_div">

      <Button size="small" @click="saveInfo" type="primary">提交</Button>
      <!--<Button size="small" v-if="id==='-1'" @click="reset" type="error">重 置</Button>-->
      <Button size="small" @click="close">关 闭</Button>

    </div>

  </div>

</template>

<script type="text/javascript">
  import api from '../../utils/api.js';
  import userInfo from './component/userInfo.vue';
  import teamAndLevel from './component/teamAndLevel.vue';
  import detailInfo from './component/detailInfo.vue';
  import accountInfo from './component/accountInfo.vue';
  import {queryROLE, queryPartnerId, queryPartnerName} from '../../utils/common.js';
  import reqDic from '../../utils/requestDictionary.js';

  export default {

    components: {
      userInfo,
      teamAndLevel,
      detailInfo,
      accountInfo,
    },
    data: function () {
      return {
        id: '',

        state: reqDic.QueryDictionary("COMMONSTATE"),

        gender: reqDic.QueryDictionary("SEX"),

        insuredArea: reqDic.QueryDictionary("INSUREAREAS"),

        info: {},
        tempInfo: {},

        showReset: false,

        levelInfo: [],
        teamInfo: [],

        isShowAccount: false,
        pageTitle: '',

      }
    },
    created: function () {

      this.id = this.$route.params.id;

      this.isShowAccount = this.id !== '-1';

      if (this.id !== '-1') {
          this.pageTitle = '修改业务员信息';
        this.query();
      } else {
          this.pageTitle = '新增业务员';
        this.allTeamInfo();
      }

    },
    methods: {
      query: function () {

        var self = this;

        api.getSalerInfo(this.id).then((res) => {

          var data = res.body.data;

          self.info = data;
          self.info.sex = self.info.sex + '';

          console.log("self", self.info);

          self.allTeamInfo();

        })
      },

      close: function () {
        this.$router.go(-1);
      },

      allTeamInfo: function () {

        var self = this;
        let data={ state: "1"}
        api.getAllTeamInfo(data).then(res => {
          self.teamInfo = res.body.data;
          self.teamInfo.push({
            id: '',
            teamName: '请选择',
          });
        })
      },

      saveInfo: function () {

        if (this.info.birthday.toString().length > 10) {
          this.info.birthday = this.formatDate(this.info.birthday.toString());
        }

        var self = this;
        if (this.id === '-1') {
          api.saveSalemanInfo(this.info).then(res => {
            if (res.body.state === "1") {
              self.$Message.success(res.body.message.info);
            } else {
              self.$Message.error(res.body.message.info);
            }
          })
        } else {
          api.updateSalerInfo(this.info).then(res => {
            if (res.body.state === "1") {
              self.$Message.success(res.body.message.info);
            } else {
              self.$Message.error(res.body.message.info);
            }
          });
        }

      },

      reset: function () {
        this.info = {}
      },

      //格式化时间
      formatDate: function (item) {
        var date = new Date(item);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var result = y + '-' + m + '-' + d;
        console.log(result);

        return result;
      },

    },
  }
</script>
